﻿using Spire.Barcode;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OfficeWatermark
{
    public partial class FormQRCode : Form
    {
        public FormQRCode()
        {
            InitializeComponent();
        }

        private string text;
        public FormQRCode(string text) : this()
        {
            this.text = text;
            this.text = "1234569";
        }

        private async Task<Image> CodeBar()
        {
            return await Task<Image>.Run(() =>
            {
                BarcodeSettings bs = new BarcodeSettings();
                bs.Type = BarCodeType.Codabar;
                bs.Data = text;
                bs.ShowText = false;
                BarCodeGenerator bg = new BarCodeGenerator(bs);
                var image = bg.GenerateImage();
                pictureBox2.Image = image;
                return image;
            });
        }

        private async Task<Image> QRCode()
        {
            return await Task<Image>.Run(() =>
            {
                BarcodeSettings bs = new BarcodeSettings();
                bs.Type = BarCodeType.QRCode;
                bs.Data = text;
                BarCodeGenerator bg = new BarCodeGenerator(bs);
                var image = bg.GenerateImage();
                pictureBox1.Image = image;
                return image;
            });
        }

        private async Task<Image> Code39Extended()
        {
            return await Task<Image>.Run(() =>
            {
                BarcodeSettings bs = new BarcodeSettings();
                bs.Type = BarCodeType.Code39Extended;
                bs.Data = text;
                BarCodeGenerator bg = new BarCodeGenerator(bs);
                var image = bg.GenerateImage();
                pictureBox3.Image = image;
                return image;
            });
        }
        private async Task<Image> ITF14()
        {
            return await Task<Image>.Run(() =>
            {
                BarcodeSettings bs = new BarcodeSettings();
                bs.Type = BarCodeType.ITF14;
                bs.Data = text;
                //vertical bar
                bs.ITF14BearerBars = ITF14BorderType.Frame;
                //horizontal bar
                //bs.ITF14BearerBars = ITF14BorderType.Bar;
                BarCodeGenerator bg = new BarCodeGenerator(bs);
                var image = bg.GenerateImage();
                pictureBox4.Image = image;
                return image;
            });
        }

        private async Task<Image> ITF6()
        {
            return await Task<Image>.Run(() =>
            {
                BarcodeSettings bs = new BarcodeSettings();
                bs.Type = BarCodeType.ITF6;
                bs.Data = text;
                BarCodeGenerator bg = new BarCodeGenerator(bs);
                var image = bg.GenerateImage();
                pictureBox5.Image = image;
                return image;
            });
        }

        private async Task<Image> USPS()
        {
            return await Task<Image>.Run(() =>
            {
                BarcodeSettings bs = new BarcodeSettings();
                bs.Type = BarCodeType.USPS;
                bs.Data = text;
                BarCodeGenerator bg = new BarCodeGenerator(bs);
                var image = bg.GenerateImage();
                pictureBox6.Image = image;
                return image;
            });
        }


        private void LoadImage()
        {
            CodeBar(); QRCode(); Code39Extended(); ITF14(); ITF6(); USPS();
            //Task.WaitAll(CodeBar(), QRCode(), Code39Extended(), ITF14(), ITF6(), USPS());
        }
        private void FormQRCode_Load(object sender, EventArgs e)
        {
            //Control.CheckForIllegalCrossThreadCalls = false;
            //LoadImage();
        }

        private void FormQRCode_Shown(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            LoadImage();
        }
    }
}
